

<template :visible.sync="visibility">
  <div>
    <el-table :data="equipmentList" highlight-current-row>
      <el-table-column label="设备编号" width="100px" prop="equipmentCode" />
      <el-table-column
        label="设备名称"
        min-width="100px"
        prop="equipmentName"
      />
      <el-table-column label="规格" width="100px" prop="specification" />
      <el-table-column label="有效期" width="160px" prop="expirationDate" />
      <el-table-column label="使用模式" width="150px">
        <template slot-scope="scope">
          <el-radio-group
            v-model="scope.row.usage"
            @click.native="pushRadio(scope.row.usage, scope.$index)"
          >
            <el-radio disabled :label="1">独占</el-radio>
            <el-radio disabled :label="0">共享</el-radio>
          </el-radio-group>
        </template>
      </el-table-column>
      <el-table-column label="复核" width="150px" v-if="currentStatus">
        <template slot-scope="scope">
          <el-checkbox
            v-model="scope.row.checkFlag"
            :disabled="scope.row.checkFlag"
            @change="checkboxChange(scope.row)"
            >确认</el-checkbox
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- <el-button
      v-if="currentTask && !currentStatus"
      style="margin: 6px 0px 0px 6px"
      size="mini"
      @click.stop="handleOpenAddEquipment()"
    >
      添加设备
    </el-button> -->
    <AddEquipment
      :visible.sync="equipmentDialogVisible"
      :update-equipment-data="equipmentList"
      @update-data="AddData"
    />
  </div>
</template>

<script>
import AddEquipment from '@/components/AddEquipment'
import { saveAllEquipment } from '@/api/mes/manufactureTask.js'
import { saveCheckEquipment } from '@/api/mes/manufactureTask.js'
export default {
  components: { AddEquipment },
  props: {
    visible: {
      required: true,
      type: Boolean
    },
    currentTask: {
      type: Object,
      default: null
    },
    equipmentData: {
      type: Array,
      default: null
    },
    currentStatus: {
      type: Boolean,
      required: false
    }
  },
  data() {
    return {
      addEquipmentDrawer: false,
      selectionData: [],
      equipmentList: [],
      equipmentDialogVisible: false
    }
  },
  computed: {
    visibility: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('update:visible', val)
      }
    }
  },
  watch: {
    visible(value) {
      if (value) {
        //
        console.log(value, 'equipmentList')
      }
    },
    equipmentData(data) {
      this.equipmentList = data
    }
  },
  mounted() {},

  methods: {
    handleOpenAddEquipment() {
      this.equipmentDialogVisible = true
    },

    AddData(data) {
      const newData = data || []
      const Arr = []
      newData.forEach((e) => {
        Arr.push({
          usage: e.usage,
          equipmentId: e.equipmentId
        })
      })

      const row = {
        manufactureTaskId: this.currentTask.manufactureTaskId,
        equipmentList: Arr
      }
      saveAllEquipment(row).then((res) => {
        this.equipmentList = Array.from(data)
      })
      this.$forceUpdate()
    },
    handleRemove(row) {
      this.equipmentList.splice(this.equipmentList.indexOf(row), 1)
    },
    checkboxChange(row) {
      this.equipmentList.forEach((e) => {
        if (e.equipmentCode === row.equipmentCode) {
          if (!e.checkFlag) {
            e.checkFlag = true
          } else {
            return
          }
        } else {
          if (e.checkFlag) {
            e.checkFlag = true
          } else {
            e.checkFlag = false
          }
        }
      })
      const data = {
        manufactureTaskCode: this.currentTask.manufactureTaskCode,
        manufactureTaskId: this.currentTask.manufactureTaskId,
        equipmentList: this.equipmentList
      }
      saveCheckEquipment(data).then((res) => {
        console.log(res, 'dhdhdhdh')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
